package com.dime.basic.service.impl;

import java.util.List;

import com.dime.basic.api.domain.DimeBasecomb;
import com.dime.basic.api.domain.DimeBasecombcriterion;
import com.dime.basic.api.domain.DimeBasegroup;
import com.dime.basic.api.model.*;
import com.dime.basic.mapper.DimeBasecombcriterionMapper;
import com.dime.basic.mapper.DimeBasegroupMapper;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.uuid.IdUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Optional;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.dime.basic.api.domain.DimeBasecombitem;
import com.dime.basic.mapper.DimeBasecombMapper;
import com.dime.basic.service.IDimeBasecombService;

/**
 * 项目组合管理Service业务层处理
 *
 * @author Tiny
 * @date 2023-09-13
 */
@Service
public class DimeBasecombServiceImpl implements IDimeBasecombService {
    @Autowired
    private DimeBasecombMapper dimeBasecombMapper;

    @Autowired
    private DimeBasegroupMapper dimeBasegroupMapper;

    @Autowired
    private DimeBasecombcriterionMapper dimeBasecombcriterionMapper;

    /**
     * 查询项目组合管理
     *
     * @param id 项目组合管理主键
     * @return 项目组合管理
     */
    @Override
    public DimeBasecomb selectDimeBasecombById(String id) {
        return dimeBasecombMapper.selectDimeBasecombById(id);
    }

    /**
     * 查询项目组合管理列表
     *
     * @param dimeBasecomb 项目组合管理
     * @return 项目组合管理
     */
    @Override
    public List<DimeBasecomb> selectDimeBasecombList(DimeBasecomb dimeBasecomb) {
        return dimeBasecombMapper.selectDimeBasecombList(dimeBasecomb);
    }

    /**
     * 新增项目组合管理
     *
     * @param dimeBasecomb 项目组合管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDimeBasecomb(DimeBasecomb dimeBasecomb) {
        dimeBasecomb.setId(IdUtils.fastUUID());
        dimeBasecomb.setCreateTime(DateUtils.getNowDate());
        dimeBasecomb.setCreateBy(SecurityUtils.getUsername());
        if (dimeBasegroupMapper.selectDimeBasegroupById(dimeBasecomb.getDimeGroupsid())!=null){
            dimeBasecomb.setGroupName(dimeBasegroupMapper.selectDimeBasegroupById(dimeBasecomb.getDimeGroupsid()).getGroupName());
        }
        int rows = dimeBasecombMapper.insertDimeBasecomb(dimeBasecomb);
        insertDimeBasecombitem(dimeBasecomb);
        return rows;
    }

    /**
     * 修改项目组合管理
     *
     * @param dimeBasecomb 项目组合管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDimeBasecomb(DimeBasecomb dimeBasecomb) {
        dimeBasecomb.setUpdateTime(DateUtils.getNowDate());
        dimeBasecomb.setUpdateBy(SecurityUtils.getUsername());
        if (dimeBasegroupMapper.selectDimeBasegroupById(dimeBasecomb.getDimeGroupsid())!=null){
            dimeBasecomb.setGroupName(dimeBasegroupMapper.selectDimeBasegroupById(dimeBasecomb.getDimeGroupsid()).getGroupName());
        }
        dimeBasecombMapper.deleteDimeBasecombitemByDimeBasecombid(dimeBasecomb.getId());
        insertDimeBasecombitem(dimeBasecomb);

        DimeBasecombcriterion dimeBasecombcriterion = new DimeBasecombcriterion();
        dimeBasecombcriterion.setCombId(dimeBasecomb.getId());
        dimeBasecombcriterion.setCombName(dimeBasecomb.getCombName());
        dimeBasecombcriterionMapper.updateDimeBasecombcriterionByCombId(dimeBasecombcriterion);

        return dimeBasecombMapper.updateDimeBasecomb(dimeBasecomb);
    }

    /**
     * 批量删除项目组合管理
     *
     * @param ids 需要删除的项目组合管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDimeBasecombByIds(String[] ids) {
        dimeBasecombMapper.deleteDimeBasecombitemByDimeBasecombids(ids);
        return dimeBasecombMapper.deleteDimeBasecombByIds(ids);
    }


    /**
     * 删除项目组合管理信息
     *
     * @param id 项目组合管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDimeBasecombById(String id) {
        dimeBasecombMapper.deleteDimeBasecombitemByDimeBasecombid(id);
        return dimeBasecombMapper.deleteDimeBasecombById(id);
    }

    /**
     * 新增项目细项中间信息
     *
     * @param dimeBasecomb 项目组合管理对象
     */
    public void insertDimeBasecombitem(DimeBasecomb dimeBasecomb) {
        List<DimeBasecombitem> dimeBasecombitemList = dimeBasecomb.getDimeBasecombitemList();
        String id = dimeBasecomb.getId();
        if (StringUtils.isNotNull(dimeBasecombitemList)) {
            List<DimeBasecombitem> list = new ArrayList<DimeBasecombitem>();
            for (DimeBasecombitem dimeBasecombitem : dimeBasecombitemList) {
                dimeBasecombitem.setId(IdUtils.fastUUID());
                dimeBasecombitem.setDimeBasecombid(id);
                dimeBasecombitem.setCreateBy(SecurityUtils.getUsername());
                dimeBasecombitem.setCreateTime(DateUtils.getNowDate());
                list.add(dimeBasecombitem);
            }
            if (list.size() > 0) {
                dimeBasecombMapper.batchDimeBasecombitem(list);
            }
        }
    }

    @Override
    public List<ComboBase> selectCombos(Combo combo) {
        return dimeBasecombMapper.selectcombos(combo);
    }

    @Override
    public Combo selectCombo(String combCode) {
        return dimeBasecombMapper.selectcombo(combCode);
    }

    @Override
    public DimeBasicDataResult checkCombosAndItems(List<ComboBase> combos) {
        DimeBasicDataResult<List<ComboBase>> re = new DimeBasicDataResult<List<ComboBase>>();
        re.setStatus(DimeBasicDataResult.Success);
        String[] combCodes = combos.stream().map(ComboBase::getCombCode).toArray(String[]::new);
        List<ComboBase> dcombs = dimeBasecombMapper.selectcomboswithitem(combCodes);

        if (!StringUtils.isNotNull(dcombs) || dcombs.isEmpty()) {
            re.setData(combos);
            re.setStatus(DimeBasicDataResult.Fail);
            return re;
        }
        re.setData(new ArrayList<>());
        for (ComboBase c : combos) {
            Optional<ComboBase> optionaldc = dcombs.stream()
                    .filter(t -> t.getCombCode().toLowerCase().equals(c.getCombCode().toLowerCase()))
                    .findFirst();
            if (!optionaldc.isPresent()) {
                re.getData().add(c);
                re.setStatus(DimeBasicDataResult.Fail);
            } else {
              //TODO: 添加检查Item
            }
        }
        return re;
    }



    @Override
    public List<ComboBase> selectCombosByCombCodes(String[] combCodes) {
       return dimeBasecombMapper.selectcomboswithitem(combCodes);
    }

    @Override
    public List<HarmCombo> selectHarmCombosByHarmIds(String[] harmIds) {
        return dimeBasecombMapper.selectcombosByHarmIds(harmIds);
    }

    @Override
    public List<DimeBasecomb> selectCombosByIds(String[] ids) {
        return dimeBasecombMapper.selectCombosByIds(ids);
    }


}
